package sqlanalysisgui;

import java.io.File;

import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

import com.cho.core.ExcelCore;
import com.cho.core.RegCore;
import com.cho.core.db.dao.TableCreate;
import com.cho.core.db.dao.TableDao;
import com.cho.core.vo.ProjectVo;

/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication {

	/* (non-Javadoc)
	 * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
	 */
	@Override
	public Object start(IApplicationContext context) throws Exception {
		Display display = PlatformUI.createDisplay();
		
		//CHO: 나중에 변경 필요
		//testInit();
		
		try {
			int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
			if (returnCode == PlatformUI.RETURN_RESTART)
				return IApplication.EXIT_RESTART;
			else
				return IApplication.EXIT_OK;
		} finally {
			display.dispose();
		}
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.equinox.app.IApplication#stop()
	 */
	@Override
	public void stop() {
		if (!PlatformUI.isWorkbenchRunning())
			return;
		final IWorkbench workbench = PlatformUI.getWorkbench();
		final Display display = workbench.getDisplay();
		display.syncExec(new Runnable() {
			@Override
			public void run() {
				if (!display.isDisposed())
					workbench.close();
			}
		});
	}
	
	private void testInit(){
		ProjectVo projectVo = new ProjectVo();
		//Properties properties = new Properties();
		try {
			//TableCreate.dropTable();//초기화
			TableCreate.runTableCreate();//초기화
			//properties.load(new FileInputStream("main.properties"));
			projectVo.setProjectName("GUI_TEST5");
			projectVo.setProjectDirectory("D:\\project\\eclipse\\workspace\\sqlAnalysisGUI\\jsp\\");
//			projectVo.setProjectDirectory("G:\\프로젝트\\Dropbox\\프로젝트\\NHN_D2 FEST\\참고자료\\workspace\\sqlAnalysisGUI\\jsp\\");
			//properties.list(System.out);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			TableCreate.runTableCreate();
			TableDao.deleteProjectInfo(projectVo.getProjectName());
			TableDao.deleteFileList(projectVo.getProjectName());
			TableDao.deleteTableList(projectVo.getProjectName());
			TableDao.deleteDbTalbeList(projectVo.getProjectName());
			
			TableDao.insertProjectInfo(projectVo);
			//TableDao.insertDbTableInfo(projectVo);
			RegCore c = new RegCore();
		
			c.readFolder(projectVo, new File(projectVo.getProjectDirectory()));
			
			ExcelCore excelCore = new ExcelCore();
			excelCore.makeExcel(projectVo.getProjectName());
			} catch (Exception e) {
				e.printStackTrace();
			}	
	}
	
/*	public static void main(String[] args) {
		Application f = new Application();
		f.testInit();
	}*/
}
